package com.stone.business.gzrwd.service;

/**
 * <pre>
 *  值班记录通用Service
 * </pre>
 */


import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


import com.stone.business.gzrwd.pojo.TGzrwd;

import com.stone.business.gzrwd.pojo.TGzrwdRw;
import com.stone.common.pojo.TFile;

import frm.ssh.service.BaseService;

public class ZbjlCommonService extends BaseService {
	
	
	
	
	/**
	 * 工作任务单新建任务
	 */
	public void saveGzrwdRw(String ids, List<HashMap<String, Object>> listRwd, TGzrwd entity, Date date) {
		
		// 新建时
		if (entity.getId() == 0) {
			
			super.save(entity);	
		
		} else {
			super.update(entity);
		}
		
		String[] indexs = ids.split(",");
		
		for (int i =0;i<listRwd.size();i++) {
			
			for (int j=0; j<indexs.length;j++) {
				
				if (Integer.parseInt(indexs[j]) == i) {
					
					TGzrwdRw rw = new TGzrwdRw();
					rw.setCjr(entity.getCjr());
					rw.setCjsj(entity.getCjsj());
					rw.setCzid(Long.parseLong(String.valueOf(listRwd.get(i).get("id"))));
					//rw.setCzurl((String)listRwd.get(i).get("czurl"));
					rw.setCzurl(listRwd.get(i).get("czurl").toString());
					rw.setModel(listRwd.get(i).get("model").toString());
					rw.setRwnr((String)listRwd.get(i).get("gzrw"));
					rw.setBdzname((String)listRwd.get(i).get("bdzname"));
					rw.setGzrwdid(entity.getId());
					rw.setSfwc("");
					super.save(rw);
					
					// 修改对应业务证明出了任务单
					
					/*// 异常故障
					if (IConstant.ZBJL_YCGZ.equals(rw.getModel())) {
						super.updateByHQL("update TYcgz set gzrwdconfirm = '1' where id = ?", new Object[]{rw.getCzid()});
					
					// 许可任务
					} else if (IConstant.ZBJL_GZPTJJL_XK.equals(rw.getModel())) {
							super.updateByHQL("update TGzptjjl set gzrwdconfirm = '1' where id = ?", new Object[]{rw.getCzid()});
							
					// 终结工作票
					} else if (IConstant.ZBJL_DRGZGJ.equals(rw.getModel())) {
							super.updateByHQL("update TGzptjjl set gzrwdconfirm2 = '1', gzrwdconfirm3 = '0' where id = ?", new Object[]{rw.getCzid()});
						
					// 操作任务
					} else if (IConstant.ZBJL_DZCZJL.equals(rw.getModel())) {
							super.updateByHQL("update TDzczjl set gzrwdconfirm = '1' where id = ?", new Object[]{rw.getCzid()});
						
					// 周安全活动情况
					} else if (IConstant.ZBJL_ZAQHDQK.equals(rw.getModel())) {
							super.updateByHQL("update TZaqhdqk set gzrwdconfirm = '1' where id = ?", new Object[]{rw.getCzid()});
						
					// 夜间巡视
					} else if (IConstant.ZBJL_XSQK.equals(rw.getModel())) {
							super.updateByHQL("update TXsqk set gzrwdconfirm = '1' where id = ?", new Object[]{rw.getCzid()});
						
					// 设备试验轮换 
					} else if (IConstant.ZBJL_SBSYLH.equals(rw.getModel())) {
						super.updateByHQL("update TSbsylh set gzrwdconfirm = '1' where id = ?", new Object[]{rw.getCzid()});
						
						// 正常巡视
					} else if (IConstant.ZBJL_XSQK_ZCXS.equals(rw.getModel())) {
						
						super.updateByHQL("update TXsqk set gzrwdconfirm = '1' where id = ?", new Object[]{rw.getCzid()});
						
					}*/
					
					break;
					
				}
			}
			
		}
	}
	
	/**
	 * 工作任务单 风险告知保存
	 */
	public void saveGzrwdFxgz(TGzrwd entity, String ids, String idsold) {
		
		super.update(entity);
		
		// 一个也没有选中
		if ("".equals(ids)) {
			super.deleteByDHql("gzrwdid = ? ", new Object[]{entity.getId()}, "TGzrwdFxgz");
			return;
		}
		
		// 风险告知
		List<HashMap<String,Object>> l = super.getListByDHql("id as id,fxgznr as text","id in (" + ids + ")",null, "", "TFxgz");
		
		// 新选择的
		String fxgzId[] = ids.split(",");
		
		// 旧的
		String fxgzIdOld[] = idsold.split(",");
		
		boolean flag = false;
		
		if (!"".equals(idsold)) {
		
			// 删除掉原来有，后来没有的。
			for (String idold : fxgzIdOld) {
				
				for (String id : fxgzId) {
					if (id.equals(idold)) {
						flag = true;
						break;
					}
				}
				
				if (!flag) {
					super.deleteByDHql("gzrwdid = ? and fxgzdicid = ?", new Object[]{entity.getId(), Long.parseLong(idold)}, "TGzrwdFxgz");
				}
				
				flag = false;
			}
		}
		
		/*// 循环插入新的
		for (String id : fxgzId) {
			
			for (String idold : fxgzIdOld) {
				if (id.equals(idold)) {
					flag = true;
					break;
				}
			}
			
			if (!flag) {
		
				for (HashMap<String,Object> map : l) {
				
					if(String.valueOf(map.get("id")).equals(id)) {
						
						TGzrwdFxgz f = new TGzrwdFxgz();
						f.setGzrwdid(entity.getId());
						f.setFxgzdicid((Long)map.get("id"));
						f.setFxgznr((String)map.get("text"));
						super.save(f);
						
						break;
					}	
				}
			}
			
			flag = false;
		}*/
		
	}
	
	/**
	 * 工作任务单未完成
	 * @param ycclqk
	 * @param id
	 * @param gzrwdid
	 */
	public void updateGzrwdRwUnOver(String ycclqk, String id, Long gzrwdid) {
		
		// 修改工作任务单 任务的状态
		super.updateByHQL("update TGzrwdRw set sfwc = '0' where id = ?", new Object[]{Long.parseLong(id)});
		
		// 修改工作任务单的未完成意见
		super.updateByHQL("update TGzrwd set ycclqk = ? where id = ?", new Object[]{ycclqk, gzrwdid});
		TGzrwdRw rw = (TGzrwdRw) super.findById(Long.parseLong(id), TGzrwdRw.class.getName());
		
		// 修改对应业务证明没出任务单
		
		/*// 异常故障
		if (IConstant.ZBJL_YCGZ.equals(rw.getModel())) {
			super.updateByHQL("update TYcgz set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
		
		// 许可任务工作票
		} else if (IConstant.ZBJL_GZPTJJL_XK.equals(rw.getModel())) {
				super.updateByHQL("update TGzptjjl set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
				
		// 终结工作票
		} else if (IConstant.ZBJL_DRGZGJ.equals(rw.getModel())) {
				super.updateByHQL("update TGzptjjl set gzrwdconfirm2 = '0', gzrwdconfirm3 = '0' where id = ?", new Object[]{rw.getCzid()});
			
		// 操作任
		} else if (IConstant.ZBJL_DZCZJL.equals(rw.getModel())) {
				super.updateByHQL("update TDzczjl set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
			
		// 周安全活动情况
		} else if (IConstant.ZBJL_ZAQHDQK.equals(rw.getModel())) {
				super.updateByHQL("update TZaqhdqk set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
			
		// 夜间巡视
		} else if (IConstant.ZBJL_XSQK.equals(rw.getModel())) {
				super.updateByHQL("update TXsqk set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
			
		// 设备试验轮换 
		} else if (IConstant.ZBJL_SBSYLH.equals(rw.getModel())) {
				super.updateByHQL("update TSbsylh set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
				
		// 正常巡视　
		} else if (IConstant.ZBJL_XSQK_ZCXS.equals(rw.getModel())) {
			super.updateByHQL("update TXsqk set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});

		}*/
		
	}
	
	/**
	 * 工作任务单取消 (删除掉一条工作任务单)
	 * @param ycclqk
	 * @param id
	 * @param gzrwdid
	 */
	public void updateGzrwdRwCancel(Long id) {

		// 修改对应业务证明没出任务单
		TGzrwdRw rw = (TGzrwdRw) super.findById(id, TGzrwdRw.class.getName());
		/*
		// 异常故障
		if (IConstant.ZBJL_YCGZ.equals(rw.getModel())) {
			super.updateByHQL("update TYcgz set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
		
		// 许可任务工作票
		} else if (IConstant.ZBJL_GZPTJJL_XK.equals(rw.getModel())) {
				super.updateByHQL("update TGzptjjl set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
				
		// 终结工作票
		} else if (IConstant.ZBJL_DRGZGJ.equals(rw.getModel())) {
				super.updateByHQL("update TGzptjjl set gzrwdconfirm2 = '0', gzrwdconfirm3 = '0' where id = ?", new Object[]{rw.getCzid()});
			
		// 操作任
		} else if (IConstant.ZBJL_DZCZJL.equals(rw.getModel())) {
				super.updateByHQL("update TDzczjl set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
			
		// 周安全活动情况
		} else if (IConstant.ZBJL_ZAQHDQK.equals(rw.getModel())) {
				super.updateByHQL("update TZaqhdqk set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
			
		// 夜间巡视
		} else if (IConstant.ZBJL_XSQK.equals(rw.getModel())) {
				super.updateByHQL("update TXsqk set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
			
		// 设备试验轮换 
		} else if (IConstant.ZBJL_SBSYLH.equals(rw.getModel())) {
				super.updateByHQL("update TSbsylh set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});
				
		// 正常巡视　
		} else if (IConstant.ZBJL_XSQK_ZCXS.equals(rw.getModel())) {
			super.updateByHQL("update TXsqk set gzrwdconfirm = '0' where id = ?", new Object[]{rw.getCzid()});

		}*/
		
		// 任务删除
		super.deleteAll(id.toString(), TGzrwdRw.class.getName());
		
	}
	
	
	/**
	 * 工作任务单删除任务
	 */
	public void deleteGzrwdRw(String ids, List<HashMap<String, Object>> listRwd) {
		
		
		String[] indexs = ids.split(",");
		
		for (int i =0;i<listRwd.size();i++) {
			
			for (int j=0; j<indexs.length;j++) {
				
				if (Integer.parseInt(indexs[j]) == i) {
					
					TGzrwdRw rw = new TGzrwdRw();
				
					rw.setCzid(Long.parseLong(String.valueOf(listRwd.get(i).get("id"))));
					rw.setCzurl((String)listRwd.get(i).get("czurl"));
					rw.setModel(listRwd.get(i).get("model").toString());
					rw.setRwnr((String)listRwd.get(i).get("gzrw"));
					rw.setBdzname((String)listRwd.get(i).get("bdzname"));
					rw.setSfwc("");
					
					
					/*// 异常故障
					if (IConstant.ZBJL_YCGZ.equals(rw.getModel())) {
						super.updateByHQL("delete TYcgz  where id = ?", new Object[]{rw.getCzid()});
					
					// 许可任务
					} else if (IConstant.ZBJL_GZPTJJL_XK.equals(rw.getModel())) {
							super.updateByHQL("delete TGzptjjl  where id = ?", new Object[]{rw.getCzid()});
							
					// 终结工作票
					} else if (IConstant.ZBJL_DRGZGJ.equals(rw.getModel())) {
							super.updateByHQL("delete TGzptjjl  where id = ?", new Object[]{rw.getCzid()});
						
					// 操作任务
					} else if (IConstant.ZBJL_DZCZJL.equals(rw.getModel())) {
							super.updateByHQL("delete TDzczjl  where id = ?", new Object[]{rw.getCzid()});
						
					// 周安全活动情况
					} else if (IConstant.ZBJL_ZAQHDQK.equals(rw.getModel())) {
							super.updateByHQL("delete TZaqhdqk  where id = ?", new Object[]{rw.getCzid()});
						
					// 夜间巡视
					} else if (IConstant.ZBJL_XSQK.equals(rw.getModel())) {
							super.updateByHQL("delete TXsqk  where id = ?", new Object[]{rw.getCzid()});
						
					// 设备试验轮换 
					} else if (IConstant.ZBJL_SBSYLH.equals(rw.getModel())) {
						super.updateByHQL("delete TSbsylh  where id = ?", new Object[]{rw.getCzid()});
						
						// 正常巡视
					} else if (IConstant.ZBJL_XSQK_ZCXS.equals(rw.getModel())) {
						
						super.updateByHQL("delete TXsqk  where id = ?", new Object[]{rw.getCzid()});
						
					}*/
					
					break;
					
				}
			}
			
		}
	}
	/**
	 * 设备缺陷记录
	 * @param id
	 */
	@SuppressWarnings("unchecked")
	public void deleteSbqxjl(String id) {

		// 附件
		List<TFile> list = super.findListByHql(" from TFile where colname = 'TAB_SBQXJL-QXZP' and tableid in (" + id + ")");
		
 		for (TFile f : list) {
 			
 			File tempFile = new File(f.getUrl() + f.getRealfilename());
 			
 			if (tempFile.exists()) {
 				tempFile.delete();
 			}
 		}
 		
 		// 删除附件表数据
 		super.deleteByDHql("colname = 'TAB_SBQXJL-QXZP' and tableid in (" + id + ")", null, "TFile");
		
		// 设备缺陷记录
		super.doDelAll(id, "TSbqxjl");
	}
	/**
	 * 倒闸操作预告任务删除
	 * @param id
	 */
	@SuppressWarnings("unchecked")
	public void deleteGzrwdRw(String id) {
		
		// 倒闸操作预告任务
		super.doDelAll(id, "TGzrwdRw");
		
		/*// 附件
		List<TFile> list = super.findListByHql(" from TFile where colname = 'TAB_DZCZJL_CZLY' " +
				" and tableid in (select id from TDzczjl where dzczygrwid in (" + id + "))");
		
 		for (TFile f : list) {
 			
 			File tempFile = new File(f.getUrl() + f.getRealfilename());
 			
 			if (tempFile.exists()) {
 				tempFile.delete();
 			}
 		}
 		
 		// 删除附件表数据
 		super.deleteByDHql("colname = 'TAB_DZCZJL_CZLY' and tableid in (select id from TDzczjl where dzczygrwid in (" + id + "))", null, "TFile");
	
		// 倒闸操作记录
		super.deleteByDHql("dzczygrwid in (" + id + ")", null, "TDzczjl");	*/
	}
}
